package ConditionPackage;

import java.util.Iterator;

import SquarePackage.Square;
import SquarePackage.TeleportableSquare;
import Dungeon.*;

public class ShaftCondition implements Condition {

	private Class<? extends Dungeon> DungeonType;
	/**
	 * default constructor sets shaft as comparing Class
	 */
	public ShaftCondition(){
		DungeonType = Shaft.class;
	}
	/**
	 * Constructot to give a ComposedDungeon with
	 * @param Root the ComposedDungeon we need to check our Squares in
	 */
	public ShaftCondition(ComposedDungeon<Square, Square, ? extends Subdungeon<? extends Square>> Root){
		this();
		setRootdungeon(Rootdungeon);
	}
	/**
	 * Constructot to give a ComposedDungeon with
	 * @param Root the rootdungeon to search our square in
	 * @param dungeonType, the type of Dungeon we want to find
	 */
	public ShaftCondition(ComposedDungeon<Square, Square, ? extends Subdungeon<? extends Square>> Root, Class<? extends Dungeon> dungeonType){
		this();
		setRootdungeon(Rootdungeon);
		setDungeonType(dungeonType);
		
	}
	/**
	 * the method that performs the Condition checking
	 * @return	True if and only if the Square is within a Dungeon of Type we specified
	 */
	@Override
	public boolean checkCondition(Square checkSquare) {
		if(checkSquare instanceof TeleportableSquare){
			for(Dungeon dungeon: Rootdungeon.getAllLevelsAndShafts()){
				Iterator<? extends Square> defaultIterator = dungeon.iterator(new DefaultCondition());
				while(defaultIterator.hasNext()){
					Square iteratorSquare = defaultIterator.next();
					if(iteratorSquare==checkSquare){
						if(dungeon.getClass()==DungeonType){
							return true;
						}
					}
				}
			}
		}
		return false;
	}
	private ComposedDungeon<Square, Square, ? extends Subdungeon<? extends Square>> Rootdungeon;
	/**
	 * method to change the rootDugeon
	 * @param rootdungeon	the Dungeon we are searching are Square in
	 */
	public void setRootdungeon(
			ComposedDungeon<Square, Square, ? extends Subdungeon<? extends Square>> rootdungeon) {
		Rootdungeon = rootdungeon;
	}
	/**
	 * the type of Dungeon we want Squares From
	 * @param dungeonType
	 */
	public void setDungeonType(Class<? extends Dungeon> dungeonType){
		DungeonType = dungeonType;
	}

}
